function submitAjax(idForm, url) {
	var form = $("#" + idForm);
	var nameUnidade = $(form).serialize();
	$.ajax({
		url : url,
		type : "POST",
		data : nameUnidade,
		success : function(retorno) {
			$("#" + idForm).html(retorno);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('Ocorreu um erro ao tentar efetuar sua requisi��o!');
		}
	});
}

function carregarCombobox(seletor, arrayDados, valorPadrao, nomeOpcaoTodos,
		nomeOpcaoSelecione, removerOpcaoSelecione) {

	if (!arrayDados) {
		$(seletor).html(
				"<option value=''>"
						+ (nomeOpcaoSelecione ? nomeOpcaoSelecione
								: "Selecione...") + "</option>");
		return;
	}

	if (removerOpcaoSelecione || nomeOpcaoTodos) {
		$(seletor).html("");
	} else {
		$(seletor).html("<option value=''>Selecione...</option>");
	}

	if (nomeOpcaoTodos) {
		$(seletor).append("<option value=''>" + nomeOpcaoTodos + "</option>");
	}

	var selecionado = "";
	for (var i = 0; i < arrayDados.length; i++) {
		if (arrayDados[i][0] == valorPadrao) {
			selecionado = "selected";
		} else {
			selecionado = "";
		}
		$(seletor).append(
				"<option value='" + arrayDados[i][0] + "' " + selecionado + ">"
						+ arrayDados[i][1] + "</option>");
	}
}

/**
 * Realiza o submit dos dados que est�o no container com nome 'idForm'
 * 
 * @param idForm
 *            ID do Container onde os elementos com os dados est�o localizados
 * @param action
 *            A URL que deve ser utilizada no Submit
 */
function submitAjax(idForm, action, idTarget, idTargetLoading) {
	
	var campos = $("#" + idForm).find("input, select, textarea").serialize();
	  	   $.ajax({
			type : "POST",
			url : action,
			data : campos,
			success : function(html) {
				// $('#' + idTarget).fadeOut("slow").html('');
				// $('#' + idTarget).fadeIn('fast').html(html);
				$('#' + idTarget).css('opacity', 0).slideDown('slow').animate({
					opacity : 1
				}, {
					queue : false,
					duration : 'slow'
				}).html(html);
			},
			beforeSend : function(jqXHR, settings) {
				if( idTargetLoading != null && idTargetLoading != ''){
					$('#' + idTargetLoading).attr('class','widget-body-ajax-loading');
				}
			},
			complete : function(jqXHR, textStatus) {
				if( idTargetLoading != null && idTargetLoading != ''){
					$('#' + idTargetLoading).removeAttr('class');
				}
			}
	  	   });
}

function submitAjaxAddTab(idForm, action, idTarget, idTargetLoading, titulo, idTab ) {
	
	
	var tabs = $("#tabsGeral").tabs();
	var  tabTemplate = "<li style='position:relative;'> <span class='air air-top-left delete-tab' style='top:7px; left:7px;'><button class='btn btn-xs font-xs btn-default hover-transparent'><i class='fa fa-times'></i></button></span><a href='#"+idTab+"' id='#a"+idTab+"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+titulo+"</a></li>"
	var tabNameExists = false;
	var posicao = 0;
	var posicaoDaTab = 0;
	$('#tabsGeral ul li a').each(function(i) {
		
		if ($.trim(this.text) == $.trim(titulo) ) {
	        tabNameExists = true;
	    }
		posicao++;
		
		if( tabNameExists ){
			posicaoDaTab = posicao;
			return false;
		}
	});

	if (tabNameExists){
		 posicaoDaTab--;
		 tabs.tabs("refresh");
	  	 tabs.tabs( "option", "active", posicaoDaTab );
	     return true;
	}else{
		tabs.find("#ultabItem").append( tabTemplate );
	}
	
	var campos = $("#" + idForm).find("input, select, textarea").serialize();
	  	   $.ajax({
			type : "POST",
			url : action,
			data : campos,
			success : function(html) {
				tabs.append("<div id='"+idTab+"'></div>");
				tabs.find('#' + idTab).html( html );
			},
			beforeSend : function(jqXHR, settings) {
				if( idTargetLoading != null && idTargetLoading != ''){
					$('#' + idTargetLoading).attr('class','widget-body-ajax-loading');
				}
			},
			complete : function(jqXHR, textStatus) {
				if( idTargetLoading != null && idTargetLoading != ''){
					$('#' + idTargetLoading).removeAttr('class');
				}
			}
	  	   });

	  	   tabs.tabs("refresh");
	  	   tabs.tabs( "option", "active", -1 );
	  
}

function submitAjaxPartial(idForm, action, idTarget, idTargetLoading) {
	var campos = $("#" + idForm).find("input, select, textarea").serialize();
	//var campos = new FormData($("#" + idForm));
	
	$.ajax({
		type : "POST",
		url : action,
		data : campos,
		success : function(html) {
			var dados = $(html).find('#' + idTarget).html();
			$('#' + idTarget).css('opacity', 0).slideDown('slow').animate({
				opacity : 1
			}, {
				queue : false,
				duration : 'slow'
			}).html(dados);
		},
		beforeSend : function(jqXHR, settings) {
			if( idTargetLoading != null && idTargetLoading != ''){
				$("form :input").attr("disabled","disabled");
				$('#' + idTargetLoading).attr('class','widget-body-ajax-loading');
			}
		},
		complete : function(jqXHR, textStatus) {
			if( idTargetLoading != null && idTargetLoading != ''){
				$("form :input").removeAttr("disabled");
				$('#' + idTargetLoading).removeAttr('class');
			}
		}
	});
	//e.preventDefault();
}
/**
 * Realiza a valida��o do campo , permitindo somente numeros
 * 
 * @param e
 *            Event que ser� executado
 * @returns {Boolean} retorna false caso seje digitado caracteres n�o numericos.
 */
function somenteNumeros(e) {
	var keynum = (window.event) ? e.keyCode : e.which;
	var keychar = String.fromCharCode(keynum);
	if (isNaN(keychar) && (keynum < 96 || keynum > 105)
			&& (keynum != 8 && keynum != 37 && keynum != 39)) {
		return false;
	}
	return true;
}

function submitAjaxEnter(idForm, action, idTarget, idTargetLoading, e){
   var tecla = (e.keyCode ? e.keyCode : e.which);
        
   if( tecla == 13 ){
	   submitAjax(idForm,action,idTarget,idTargetLoading);
    }
}

function dateMask(inputData, e) {
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else
		// Outros Browsers
		var tecla = e.which;

	if (tecla >= 47 && tecla < 58) { // numeros de 0 a 9 e "/"
		var data = inputData.value;
		if (data.length == 2 || data.length == 5) {
			data += '/';
			inputData.value = data;
		}
	} else if (tecla == 8 || tecla == 0) // Backspace, Delete e setas
											// direcionais(para mover o cursor,
		return true;
	else
		return false;
}

/**
 * Retira os espa�os de uma String
 * 
 * @param str
 * @returns
 */
function retirarEspacos(str) {
	return str.replace(/^\s+|\s+$/g, "");
}